<template>
    <el-dialog @open="open" v-model="isOpen" :title="props.openType==='add'?'添加秒杀活动商品关联':'修改秒杀活动商品关联'" @close="close" style="width: 30%;min-width: 300px">
        <el-form ref="dataForm" :rules="rules" :model="form" label-width="150px" align="center">
            <el-form-item label="活动场次id" prop="promotionSessionId">
                <el-input disabled  v-model="form.promotionSessionId"/>
            </el-form-item>
            <el-form-item label="商品id" prop="skuId">
                <el-input :disabled ="props.openType==='edit'"  v-model="form.skuId"/>
            </el-form-item>
            <el-form-item label="秒杀价格" prop="seckillPrice">
                <el-input-number v-model="form.seckillPrice" :min="0*1"  controls-position="right" />
            </el-form-item>
            <el-form-item label="秒杀总量" prop="seckillCount">
                <el-input-number v-model="form.seckillCount" :min="1*1"  controls-position="right" />
            </el-form-item>
            <el-form-item label="每人限购数量" prop="seckillLimit">
                <el-input-number v-model="form.seckillLimit" :min="1*1"  controls-position="right" />
            </el-form-item>
            <el-form-item label="排序" prop="seckillSort">
                <el-input-number v-model="form.seckillSort" :min="1*1"  controls-position="right" />
            </el-form-item>
            <el-form-item>
                <el-button v-show='props.openType==="add"' type="primary" activity  @click="addSmsSeckillSkuRelation">添加</el-button>
                <el-button v-show="props.openType==='edit'" type="primary" activity  @click="updateSmsSeckillSkuRelation">更新</el-button>
                <el-button @click="close" type="success" activity >取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
    import {ref,reactive} from 'vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    const  emits = defineEmits(['close','reload'])

    let dataForm = ref(null)
    let props = defineProps(['openType','id'])
    let form = reactive({})
    let isOpen = ref(false)
    defineExpose({isOpen})

    const rules = reactive({
            skuId: [{required: true,message: '请输入商品id', trigger: 'blur'},],
            seckillPrice: [{required: true,message: '请输入秒杀价格', trigger: 'blur'},],
            seckillCount: [{required: true,message: '请输入秒杀总量', trigger: 'blur'},],
            seckillLimit: [{required: true,message: '请输入每人限购数量', trigger: 'blur'},],
            seckillSort: [{required: true,message: '请输入排序', trigger: 'blur'},],
    })
    function open(){
        if(props.openType==='edit'){
            request.get(`/api/smsSeckillSession/getSmsSeckillSkuRelationById/${props.id}`).then(resp => {
                for(let key in resp){
                    form[key] = resp[key]
                }
            })
        }else if(props.openType==="add"){
            form.promotionSessionId = props.id
            form.seckillCount = 1
            form.seckillPrice = 1
            form.seckillLimit = 1
            form.seckillSort = 1
        }
    }

    function close(){
        emits("close")
        dataForm.value.resetFields()//重置表单校验
    }

    function addSmsSeckillSkuRelation(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/smsSeckillSession/addSkuRelation",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '添加成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
    function updateSmsSeckillSkuRelation(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/smsSeckillSession/updateSkuRelation",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '修改成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
</script>

<style scoped>

</style>